package com.bread.tliaswebmanagement.controller;

import com.bread.tliaswebmanagement.anno.Log;
import com.bread.tliaswebmanagement.pojo.Clazz;
import com.bread.tliaswebmanagement.pojo.ClazzQueryParam;
import com.bread.tliaswebmanagement.pojo.PageResult;
import com.bread.tliaswebmanagement.pojo.Result;
import com.bread.tliaswebmanagement.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("新增班级: {}",clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询全部班级");
        List<Clazz> all = clazzService.findAll();
        return Result.success(all);
    }

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级：{}",id);
        if (clazzService.deleteById(id)) {
            return Result.success();
        }else {
            return Result.error("该班级下有学生, 不能删除");
        }
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询班级: {}",id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级: {}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("分页查询:{}", clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

}
